package dao

import (
	"service/app/dto"
	"service/app/mode"
)

type AdminDao struct {
	BaseDao
}

var adminDao *AdminDao

func NewAdminDao() *AdminDao {
	if adminDao == nil {
		adminDao = &AdminDao{
			BaseDao: newBaseDao(),
		}
	}
	return adminDao
}

func (m *AdminDao) AdminRmnotUseLinkD(SysList *dto.SysListAc) error {
	var urlList []string
	for _, v := range SysList.SysList {
		urlList = append(urlList, v.Fullpath)
	}
	var resList []mode.Db_pm
	// 硬删除 notin
	tx := m.Orm.Begin()
	// err := m.Orm.Model(&mode.Db_pm{}).Where("fullpath NOT IN (?)", urlList).Find(&resList).Error
	err := tx.Model(&mode.Db_pm{}).Where("fullpath NOT IN (?)", urlList).Find(&resList).Error
	if err != nil {
		return err
	}
	for _, v := range resList {
		var cache mode.Db_pm
		if err := tx.Unscoped().Model(&mode.Db_pm{}).Where("id = ?", v.ID).Delete(&cache).Error; err != nil {
			tx.Rollback() // 回滚事务
			return err
		}
	}
	// m.Orm.Preload("Roles").Delete(&resList)
	tx.Commit()
	return nil
}

// func (m *AdminDao) AdminGetRole(page *dto.ReqPaginat) []mode.CasbinRule {
// 	var IadminDao []mode.CasbinRule
// 	m.Orm.Model(&IadminDao).Where("ptype = ?", "p").Scopes(page.PaginatFn(m.Orm)).Find(&IadminDao)
// 	return IadminDao
// }
// func (m *AdminDao) RolesByUsernameD(Username string) (*[]string, error) {
// 	var IadminDao []mode.CbRole
// 	var newlist []string
// 	err := m.Orm.Model(mode.CasbinRule{}).Where("ptype = 'g' and V0 = ? ", Username).Find(&IadminDao).Error
// 	if err != nil {
// 		return nil, err
// 	}
// 	if len(IadminDao) > 0 {
// 		for _, v := range IadminDao {
// 			newlist = append(newlist, v.V1)
// 		}
// 	}
// 	return &newlist, nil
// }
